﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import com.adobe.serialization.json.JSON;
	import flash.text.TextFormat;
	import flash.events.*;
	/**
	 * ...
	 * @author ...
	 */
	public class OtherFunction 
	{
		public static var TimerCount:Timer = new Timer(1000);
		public static var strShow : String = "";
		public static function GoldDreamCity_Click(evt : MouseEvent) {
			//SpecialAlert("Các bạn chú ý") ;
			//return ;
			var popup : MovieClip = MovieClip(new Cocaro.Popup_ConvertGold) ;
			popup.DoiGold_btn.addEventListener(MouseEvent.CLICK, ConvertGold_Click) ;
			popup.Close_btn.addEventListener(MouseEvent.CLICK, CloseConvertGold_Click) ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			popup.Value_txt.setStyle("textFormat", tfStyle);
			popup.name = "DreamCityGold" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		public static function CheckValid(str : String) : Boolean {
			for (var i:int = 0; i < str.length; i++) 
			{
				if ((str.charAt(i) != "0") && (str.charAt(i) != "1") && (str.charAt(i) != "2") && (str.charAt(i) != "3") && (str.charAt(i) != "4") 
					&& (str.charAt(i) != "5") && (str.charAt(i) != "6") && (str.charAt(i) != "7") && (str.charAt(i) != "8") && (str.charAt(i) != "9")) return false ;
			}
			return true ;
		}
		public static function ConvertGold_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DreamCityGold")) ;
			if (popup == null) return ;	
			if (!CheckValid(popup.Value_txt.text)) {
				BasicFunction.ShowAlert("Số bạn nhập không hợp lệ!") ;
				return ;
			}
			if (parseInt(popup.Value_txt.text) * 300 > Cocaro.currPlayer.MarkLQ) {
				BasicFunction.ShowAlert("Bạn không có đủ LQ để đổi!") ;
				return ;
			}
			var obj : Object = new Object() ;
			obj.evt = "cvd" ;
			obj.LQ = parseInt(popup.Value_txt.text) * 300 ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			var mc : MovieClip = MovieClip(new Loading()) ;
			mc.x = 0 ;
			mc.y = 0 ;
			mc.name = "Loading" ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}
		public static function CloseConvertGold_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DreamCityGold")) ;
			if (popup == null) return ;			
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		public static function ConvertGoldOK(obj : Object) {			
			BasicFunction.RemoveHideLoading() ;
			BasicFunction.ShowAlert("Bạn đã đổi thành công " + (Cocaro.currPlayer.MarkLQ - obj.LQ).toString() + " LQ sang Gold của DreamCity") ; 
			Cocaro.currPlayer.MarkLQ = obj.LQ ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
		}
		public static function Close_Click(evt : MouseEvent) {
			if (TimerCount != null)
				if (TimerCount.running)
					TimerCount.stop() ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("SpecialAlert")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		
		public static function StartTimerCount(evt:TimerEvent) : void { 
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("SpecialAlert")) ;
			if (TimerCount.currentCount < 20)				
				popup.alpha = TimerCount.currentCount * 0.04 ;
			else if (TimerCount.currentCount < 40)
				popup.alpha = 0.8 + (TimerCount.currentCount - 20) * 0.01 ;
			else if (TimerCount.currentCount > 40)
				popup.alpha = 1 - (TimerCount.currentCount - 30) * 0.05 ;
			if (TimerCount.currentCount == TimerCount.repeatCount) {
				TimerCount.stop() ;
				while(popup != null) {
					Cocaro.mainStage_mc.removeChild(popup) ;
					popup = MovieClip(Cocaro.mainStage_mc.getChildByName("SpecialAlert")) ;
				}
			}
		}
		public static function SpecialAlert(str : String) {
			var popup1 : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("SpecialAlert")) ;
			while(popup1 != null) {
				Cocaro.mainStage_mc.removeChild(popup1) ;
				popup1 = MovieClip(Cocaro.mainStage_mc.getChildByName("SpecialAlert")) ;
			}
			strShow = str ;
			var popup : MovieClip = MovieClip(new Cocaro.Popup_SpecialAlert) ;
			popup.Alert_txt.text = strShow;
			popup.name = "SpecialAlert" ;
			popup.x = 0 ;
			popup.y = 0 ;
			popup.alpha = 0.04 ;			
			Cocaro.mainStage_mc.addChild(popup) ;
			TimerCount.stop() ;
			TimerCount = new Timer(100) ;
			TimerCount.repeatCount = 60;
			TimerCount.addEventListener(TimerEvent.TIMER, StartTimerCount);			
			TimerCount.start() ;
		}
	}
	
}